// pages/music-detail/music-detail.js
const {http} = require("../../models/http")
const {timepro,numpro} = require("../../utils/music")
Page({
  data: {
    playlist:[],
    obj1:[],
    tracks:[],
    playStates:{},
    prevID:"",
  },

  onLoad: function (options) {
    let {id} =options;
    let url = `playlist/detail?id=${id}`;
    http({
      url,
      success:res=>{
        let playlist = res.data.playlist;
        playlist.playCount = numpro(playlist.playCount);
        playlist.subscribedCount = numpro(playlist.subscribedCount);
        playlist.commentCount = numpro(playlist.commentCount);
        playlist.shareCount = numpro(playlist.shareCount)

        /* if(playlist.name.length>14){
          playlist.name = play.name.slice(0,14)
        } */
        var tracks = res.data.playlist.tracks;
        tracks.forEach(item=>{
          var ar = item.ar;
          item.ar = {ar};
          if(item.ar.ar[0].name.length>6){
            item.ar.ar[0].name = item.ar.ar[0].name.slice(0,6)+"...";
          }
          if(item.al.name.length>5){
            item.al.name = item.al.name.slice(0,5)+"...";
          }
          item.dt = timepro(item.dt)
          this.setData({
            playlist,
            tracks
          })
        })
      }
    })
  },

  handleClick(event){
    let id = event.currentTarget.dataset.id;
    let playStates = this.data.playStates;
    if(id !=this.data.prevID){
      playStates[id] = true;
      for(let key in playStates){
        if(key !=id){
          playStates[key] = false;
        }
      }
      this.setData({
        playStates,
        prevID:id
      })
    }else{
      playStates[id] =!playStates[id];
      this.setData({
        playStates
      })
    }
  }
})